<?php

namespace App\Admin\Controllers;

use App\Models\Distributor;
use App\Models\DistributorBank;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Arr;
use Illuminate\Support\MessageBag;

class DistributorBankController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '分销员进件';
    
    protected $status = ['1' => '待开户', 2 => "已开户"];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DistributorBank());
        
        $grid->column('distributor.name', __('Distributor id'));
        $grid->column('name', __('Name'));
        $grid->column('id_card_no', __('Id card no'));
//        $grid->column('id_card_start_date', __('Id card start date'));
//        $grid->column('id_card_expire_date', __('Id card expire date'));
//        $grid->column('mobile', __('Mobile'));
        $grid->column('email', __('Email'));
        $grid->column('id_card_just_url', __('Id card just url'))->image('', '60');
        $grid->column('id_card_back_url', __('Id card back url'))->image('', '60');
        $grid->column('bank_card_just_url', __('Bank card just url'))->image('', '60');
        $grid->column('bank', __('Bank'));
//        $grid->column('bank_code', __('Bank code'));
//        $grid->column('bank_name', __('Bank name'));
//        $grid->column('bank_no', __('Bank no'));
        $grid->column('status', __('Status'))->using($this->status)
	        ->dot([1 => 'danger', 2 => 'success']);
        $grid->column('created_at', __('Created at'));
        
        $grid->disableCreateButton();
        
        $grid->filter(function ($f) {
            $f->disableIdFilter();
            $f->like('name', __('Name'));
            $f->like('mobile', __('Mobile'));
            $f->equal('status', __('Status'))
                ->radio(Arr::add($this->status, '', '全部'));
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(DistributorBank::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('distributor_id', __('Distributor id'));
        $show->field('name', __('Name'));
        $show->field('id_card_no', __('Id card no'));
        $show->field('id_card_start_date', __('Id card start date'));
        $show->field('id_card_expire_date', __('Id card expire date'));
        $show->field('mobile', __('Mobile'));
        $show->field('email', __('Email'));
        $show->field('id_card_just_url', __('Id card just url'));
        $show->field('id_card_back_url', __('Id card back url'));
        $show->field('bank_card_just_url', __('Bank card just url'));
        $show->field('bank', __('Bank'));
        $show->field('bank_code', __('Bank code'));
        $show->field('bank_name', __('Bank name'));
        $show->field('bank_no', __('Bank no'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new DistributorBank());

        $form->select('distributor_id', __('Distributor id'))
	        ->options(Distributor::all()->pluck('name', 'id'));

        $form->text('name', __('Name'));
        $form->text('id_card_no', __('Id card no'));
        $form->datetime('id_card_start_date', __('Id card start date'));
        $form->datetime('id_card_expire_date', __('Id card expire date'));
        $form->mobile('mobile', __('Mobile'));
        $form->email('email', __('Email'));
        $form->image('id_card_just_url', __('Id card just url'));
        $form->image('id_card_back_url', __('Id card back url'));
        $form->image('bank_card_just_url', __('Bank card just url'));
        $form->text('bank', __('Bank'));
        $form->text('bank_code', __('Bank code'));
        $form->text('bank_name', __('Bank name'));
        $form->text('bank_no', __('Bank no'));
        $form->radio('status', __('Status'))
	        ->options($this->status)
	        ->default(1);
        
        $form->disableCreatingCheck();
        $form->disableReset();
        
        // 判断是否为重复操作
        $form->submitted(function (Form $form){
            if($form->model()->status == 2 && $form->status == 1){
            	$error = new MessageBag([
			        'title'   => '标题...',
			        'message' => '请不要重复操作....',
			    ]);
			
			    return back()->with(compact('error'));
            }
        });

        return $form;
    }
}
